#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 105;

struct Edge{
	int from,to,cost;
};
Edge e[maxn*maxn];
int fat[maxn],tot=0,k=0;


int father(int x){
	if(fat[x]!=x)	fat[x] = father(fat[x]);
	return fat[x];
}

void unionn(int x,int y){
	int fa = father(x);
	int fb = father(y);
	if(fa!=fb)	fat[fa] = fb;
}

int cmp(const Edge &a,const Edge &b){
	if(a.cost<b.cost)	return 1;
	else 
		return 0;
}

int main(){
	int n,x,m=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>x;
			if(x!=0){
				m++;
				e[m].from = i;e[m].to = j;e[m].cost = x;
			}
		}
	}
	
	for(int i=1;i<=n;i++){
		fat[i] = i;
	}
	
	sort(e+1,e+m+1,cmp);
	
	for(int i=1;i<=m;i++){
		if(father(e[i].from)!=father(e[i].to)){
			unionn(e[i].from,e[i].to);
			tot+=e[i].cost;
			k++;
		}
		if(k==n-1)	break;
	}
	
	cout<<tot<<endl;
}
